package com.zhoug.common3.adapters.recycler;

import android.content.Context;

import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 动画滚动到RecyclerView顶部的滚动器
 *
 * @Author 35574
 * @Date 2020/9/17
 * @Description
 */
public class TopLinearSmoothScroller extends LinearSmoothScroller {

    public TopLinearSmoothScroller(Context context) {
        super(context);
    }

    @Override
    protected int getHorizontalSnapPreference() {
        return SNAP_TO_START;//具体见源码注释
    }

    @Override
    protected int getVerticalSnapPreference() {
        return SNAP_TO_START;//具体见源码注释
    }

    /**
     * 滚动待position位置且置顶
     * @param recyclerView
     * @param position
     */
    public void smoothScrollToPositionTop(RecyclerView recyclerView, int position) {
        setTargetPosition(position);
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager != null) {
            layoutManager.startSmoothScroll(this);
        }
    }

}